package funcdemo

import (
	"basego/classdemo"
	"fmt"
)



func Ff() {
	lambdemo()

	p := &classdemo.Person{}
	p.FuncNam(1, "lambda")
	//p.sayHi() sayHi 是私有方法，包外无法访问
}

func lambdemo() {
	// 语法 func(参数) 返回值类型 {}

	//用lambda定义一个匿名函数
	f := func(a int) int {
		return a * a
	}
	fmt.Println(f(2))

	func(x string) {
		fmt.Println("this is , ", x)
	}("lambda")

	applyFunc(2, func(a int) int {
		return a * a
	})

	f1 := createCounter()
	fmt.Println("haha", f1())
	fmt.Println("haha", f1())

}

// 高阶函数示例
func applyFunc(number int, f func(int) int) []int {
	result := make([]int, 3)
	result = append(result, f(number))
	return result
}

// 闭包示例
func createCounter() func() int {
	count := 0
	return func() int {
		count++
		return count
	}
}
